package com.dwxt.cdms.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dwxt.cdms.dao.ElectronicFenceMapper;
import com.dwxt.cdms.entity.ElectronicFence;
import com.dwxt.cdms.entity.ElectronicFenceExample;
import com.dwxt.cdms.util.ElectronicFenceUtil;
import com.dwxt.cdms.util.JsonUtil;
import com.dwxt.cdms.util.UtilDao.Point;
import com.dwxt.common.base.BaseResult;
import com.dwxt.common.base.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ElectronicFenceService {

    @Autowired
    private ElectronicFenceMapper electronicFenceMapper;

    public BaseResult insertOrUpdate(ElectronicFence electronicFence){
        int i=0;
        StringBuilder stringBuilder = new StringBuilder();
        //二维数组转换
        if (electronicFence.getElectronicAreaDouble()!=null){
            electronicFence.setElectronicArea(JsonUtil.doubleArrayToString(electronicFence.getElectronicAreaDouble()));
        }
        if (electronicFence.getId()!=null && electronicFence.getCustomerId()!=null){
            i = electronicFenceMapper.updateByPrimaryKey(electronicFence);
        }else {
            i = electronicFenceMapper.insertSelective(electronicFence);
        }
        if (i==1){
            return BaseResult.ok();
        }else {
            return BaseResult.build(StatusCode.FAIL);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public BaseResult delete(Integer id){
        int i=0;
        ElectronicFence electronicFence = electronicFenceMapper.selectByPrimaryKey(id);
        if (electronicFence.getStatus()==0){
            i = electronicFenceMapper.deleteByPrimaryKey(id);
            return BaseResult.ok();
        }else {
            return BaseResult.build(StatusCode.CAN_NOT_DELETE);
        }
    }

    /**
     * 前端查询
     * @return
     */
    public ElectronicFence selectFenceById(Integer id){
        ElectronicFence electronicFence = electronicFenceMapper.selectByPrimaryKey(id);
        String electronicArea = electronicFence.getElectronicArea();
        JSONArray jsonArray = JSONArray.parseArray(electronicArea);
        double[][]doubles=new double[jsonArray.size()][2];
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            //x
            doubles[i][0]=Double.valueOf(jsonObject.get("经度").toString().trim());
            doubles[i][1]=Double.valueOf(jsonObject.get("纬度").toString().trim());
        }
        electronicFence.setElectronicAreaDouble(doubles);
        return electronicFence;
    }

    /**
     * @方法名：selectFenceByCustomerId
     * @描述： 根据用户查询电子围栏设置
     * @作者： kjz
     * @日期： Created in 2020/9/25 15:09
     */
    public ElectronicFence selectFenceByCustomerId(Integer customerId){
        ElectronicFence electronicFence = electronicFenceMapper.selectFenceByCustomerId(customerId);
        if(null!=electronicFence){
            String electronicArea = electronicFence.getElectronicArea();
            JSONArray jsonArray = JSONArray.parseArray(electronicArea);
            double[][]doubles=new double[jsonArray.size()][2];
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                //x
                doubles[i][0]=Double.valueOf(jsonObject.get("经度").toString().trim());
                doubles[i][1]=Double.valueOf(jsonObject.get("纬度").toString().trim());
            }
            electronicFence.setElectronicAreaDouble(doubles);
        }
        return electronicFence;
    }

    /**
     * 判断电子围栏是否报警
     * @param point
     * @param customerId
     */
    public boolean isWarning(Point point, Integer customerId){
        ElectronicFence electronicFence1 = electronicFenceMapper.selectFenceByCustomerId(customerId);
        if (electronicFence1==null || electronicFence1.getStatus().equals(0)) {
            return false;
        }
        ElectronicFenceExample electronicFenceExample = new ElectronicFenceExample();
        ElectronicFenceExample.Criteria criteria = electronicFenceExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        List<ElectronicFence> electronicFences = electronicFenceMapper.selectByExample(electronicFenceExample);
        if (electronicFences.size()==1){
            ElectronicFence electronicFence = electronicFences.get(0);
            JSONArray jsonArray = JSONArray.parseArray(electronicFence.getElectronicArea());
            double[][]doubles=new double[jsonArray.size()][2];
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                doubles[i][0]=Double.valueOf(jsonObject.get("经度").toString());
                doubles[i][1]=Double.valueOf(jsonObject.get("纬度").toString());
            }
        //是否报警
           return ElectronicFenceUtil.IsInPolygon(point,doubles,doubles.length);
        }else {
            return false;
        }
    }

    /**
     * @方法名：isEnabledElectronicFence
     * @描述： 更新电子围栏启用状态
     * @作者： kjz
     * @日期： Created in 2020/9/25 17:29
     */
    public int isEnabledElectronicFence(Integer customerId) {
        ElectronicFence electronicFence = this.selectFenceByCustomerId(customerId);
        if(electronicFence.getStatus()==0){
            electronicFence.setStatus(1);
        }else{
            electronicFence.setStatus(0);
        }
        return electronicFenceMapper.updateByPrimaryKeySelective(electronicFence);
    }
}
